package com.excellence.service.impl;

import com.excellence.mapper.UserMapper;
import com.excellence.pojo.User;
import com.excellence.service.UserService;
import com.excellence.utils.SaveFilesPath;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;
import java.util.List;

/**
 * @author 沈秉璋
 * @ClassName UserServiceImpl
 * @Description TODO
 * @date 2024/4/8 10:51
 * @Version 1.0
 */
@Slf4j
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    UserMapper userMapper;

    @Override
    public int deleteByKey(Integer id) {
        User record = userMapper.selectByPrimaryKey(id);
        record.setDeleted((short) 1);
        return userMapper.updateByPrimaryKey(record);
    }

    @Override
    public int deleteByPrimaryKey(Integer id) {
        return userMapper.deleteByPrimaryKey(id);
    }

    @Override
    public String insert(User record, List<MultipartFile> files) {
        User user = selectByUsername(record.getUserUsername());
        if (user != null) {
            return "用户已存在!!!";
        }
        if (files != null) {
            record.setUserAvatar(SaveFilesPath.saveFilesPath(files));
        }
        record.setCreateTime(new Date());
        record.setUpdateTime(new Date());
        record.setDeleted((short) 0);
        userMapper.insert(record);
        return "注册用户成功";
    }

    @Override
    public int insertSelective(User record, List<MultipartFile> files) {
        if (files != null) {
            record.setUserAvatar(SaveFilesPath.saveFilesPath(files));
        }
        record.setUserAvatar("https://osssavefiles.oss-cn-beijing.aliyuncs.com/%E5%A4%B4%E5%83%8F%20%E7%94%B7%E5%AD%A9.png");
        record.setDeleted((short) 0);
        record.setCreateTime(new Date());
        record.setUpdateTime(new Date());
        try {
            userMapper.insertSelective(record);
        } catch (Exception e) {
            return 0;
        }
        return 1;
    }

    @Override
    public User selectByPrimaryKey(Integer id) {
        return userMapper.selectByPrimaryKey(id);
    }

    //根据账号查用户
    @Override
    public User selectByUsername(String userUsername) {
        return userMapper.selectByUsername(userUsername);
    }

    @Override
    public List<User> selectAll() {
        return userMapper.selectAll();
    }

    @Override
    public int updateByPrimaryKeySelective(User record, List<MultipartFile> files) {
        if (files != null) {
            record.setUserAvatar(SaveFilesPath.saveFilesPath(files));
        }
        record.setUpdateTime(new Date());
        return userMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(User record, List<MultipartFile> files) {
        if (files != null) {
            record.setUserAvatar(SaveFilesPath.saveFilesPath(files));
        }
        record.setUpdateTime(new Date());
        return userMapper.updateByPrimaryKey(record);
    }

    @Override
    public User userLogin(User user) {
        return userMapper.userLogin(user);
    }
}
